package com.steel.yfmall.cloud.assistant.common.impl;


import com.alibaba.fastjson.JSON;
import com.steel.yfmall.cloud.assistant.common.CacheRedisService;
import com.steel.yfmall.cloud.assistant.bean.AssistantConstants;
import com.steel.yfmall.cloud.assistant.common.ChannelInfoService;
import com.steel.yfmall.cloud.common.api.PlatformCommonPublicInfoService;
import com.steel.yfmall.cloud.common.bean.param.APIParaDepotInfoBean;
import com.steel.yfmall.cloud.common.bean.result.APIResultDepotInfoBean;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service("channelInfoService")
public class ChannelInfoServiceImpl implements ChannelInfoService {


    @Resource
    private PlatformCommonPublicInfoService platformCommonPublicInfoService;

    @Resource
    private CacheRedisService cacheRedisService;

	/**
	 * 获取仓库信息Map
	 *
	 * @return com.steel.yfmall.cloud.components.utils.base.APIMsgBean<java.util.HashMap < java.lang.String, com.steel.yfmall.cloud.common.bean.result.APIResultDepotInfoBean>>    返回类型
	 * @Title: getDepotInfoBeanMapByPara
	 * @Description: (这里用一句话描述这个方法的作用)
	 * @Param apiParaBean 参数说明
	 * @author yuchaohua
	 * @date 2020/12/3 15:40
	 */
	@Override
	public HashMap<String, APIResultDepotInfoBean> getDepotInfoBeanMapByPara(APIParaDepotInfoBean apiParaBean) {
        HashMap<String, APIResultDepotInfoBean> returnBean = null;
		try {
            APIMsgBean<HashMap<String, APIResultDepotInfoBean>> apiMsgBean = platformCommonPublicInfoService.getDepotInfoBeanMapByPara(apiParaBean);
            if (apiMsgBean != null && apiMsgBean.getData() != null){
                returnBean = apiMsgBean.getData();
            }
            // 如果不为空 放入到redis中
            if (returnBean != null && returnBean.size() > 0){
                for (Map.Entry<String, APIResultDepotInfoBean> entry : returnBean.entrySet()){
                    cacheRedisService.setRedisData(getDepotKey(entry.getKey()), AssistantConstants.REDIS_SAVE_TIME, JSON.toJSONString(entry.getValue()));
                }
            }
		} catch (Exception e) {
			log.error("function getDepotInfoBeanMapByPara", e);
		}
		return returnBean;
	}

    /**
     * 从redis中获取仓库信息
     * @Title: getDepotInfoBeanRedisByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param depotCode 参数说明
     * @return com.steel.yfmall.cloud.common.bean.result.APIResultDepotInfoBean    返回类型
     * @author yuchaohua
     * @date 2020/12/5 23:04
     */
    @Override
    public APIResultDepotInfoBean getDepotInfoBeanRedisByPara(String depotCode) {
        APIResultDepotInfoBean returnBean = null;
        // 在redis中获取值
        String CACHEKEY = getDepotKey(depotCode);
        try {
            String value = cacheRedisService.getCommonStringData(CACHEKEY);
            if (StringUtils.isNotBlank(value)) {
                // redis中值不为空则转换为对象
                returnBean = JSON.parseObject(value, APIResultDepotInfoBean.class);
            }
        } catch (Exception e) {
            log.error("function getProductCategoryWeightListByPara", e);
        }
        return returnBean;
    }

	/**
	 * 获取仓库信息
	 * @Title: getDepotInfoBeanByPara
	 * @Description: (这里用一句话描述这个方法的作用)
	 * @Param apiParaBean 参数说明
	 * @return com.steel.yfmall.cloud.common.bean.result.APIResultDepotInfoBean    返回类型
	 * @author yuchaohua
	 * @date 2020/12/5 22:42
	 */
    @Override
    public APIResultDepotInfoBean getDepotInfoBeanByPara(APIParaDepotInfoBean apiParaBean) {
        APIResultDepotInfoBean returnBean = null;
        try {
            // redis中值不为空则转换为对象
            returnBean = getDepotInfoBeanRedisByPara(apiParaBean.getDepotCode());
            if (null != returnBean) {
                return returnBean;
            }
            APIMsgBean<APIResultDepotInfoBean> apiMsgBean = platformCommonPublicInfoService.getDepotInfoBeanByPara(apiParaBean);
            if (apiMsgBean != null && apiMsgBean.getData() != null){
                returnBean = apiMsgBean.getData();
                // 在redis中获取值
                cacheRedisService.setRedisData(getDepotKey(apiParaBean.getDepotCode()), AssistantConstants.REDIS_SAVE_TIME, JSON.toJSONString(returnBean));
            }
        } catch (Exception e) {
            log.error("function getDepotInfoBeanMapByPara", e);
        }
        return returnBean;
    }

    /**
     * key
     * @param depotCode
     * @return
     */
    public String getDepotKey(String depotCode){
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put("siteCode", AssistantConstants.DEFALUT_SITECODE);
        params.put("depotCode", depotCode);
        // 在redis中获取值
        return cacheRedisService.generatorIndex("getProductDepotByPara", params);
    }

}
